﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;

namespace MonitorAPI.Controllers
{
    public class TestController : ApiController
    {
        [HttpGet]
        [Route("api/test/shutup")]
        public string shutup()
        {
            return "Yes I will be quiet;";
        }

        [HttpGet]
        [Route("api/authors/{authorId:int}/books/{authorName}")]
        public IEnumerable<string> GetByAuthor(int authorId, string authorName)
        {
            var s = new string[] { "12", "were", "dsfew", authorName };
            return s;
        }

        public IEnumerable<string> Get(int authorId, string authorName)
        {
            var s = new string[] { "12", "were", "dsfew", authorName };
            return s;
        }


        [HttpGet]
        [Route("api/test/noget")]
        public HttpResponseMessage NoGet()
        {
            // Get a list of products from a database.
            IEnumerable<Product> products = new Product[] { new Product() { Name = "弱智产品" } };

            var jsonstr = Newtonsoft.Json.JsonConvert.SerializeObject(products);

            // Write the list to the response body.
            HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, jsonstr);
            return response;
        }

        [HttpGet]
        [Route("api/test/try")]
        public IHttpActionResult newtry()
        {
            var t = new textTry(Request);
            return t;
        }

        public class textTry : IHttpActionResult
        {
            private HttpRequestMessage _request;
            public textTry(HttpRequestMessage msg)
            {
                this._request = msg;
            }

            public System.Threading.Tasks.Task<HttpResponseMessage> ExecuteAsync(System.Threading.CancellationToken cancellationToken)
            {
                var msg = new HttpResponseMessage()
                {
                    Content = new StringContent("你来干我?"),
                    RequestMessage = this._request

                };

                IEnumerable<Product> products = new Product[] { new Product() { Name = "弱智产品" } };

                var s = Task<HttpResponseMessage>.Run(()
                    => msg.RequestMessage.CreateResponse(HttpStatusCode.OK, products));
                return s;
                //return Task.FromResult<HttpResponseMessage>(msg);
            }
        }

        public class Product
        {
            public string Name { get; set; }
        }
    }
}
